/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package IsdComponent;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Shameera
 */
public class ReadIsdXml {
    private final String xmlFile;
    private Node rootNode;

//    private final String defaultPath = "";

    public ReadIsdXml(String xmlFile) {
        this.xmlFile = xmlFile;
        parseXmlFile();
    }

    private void parseXmlFile() {
        File file = new File(xmlFile);
        if (file.exists()) {
            try {
                // Create a factory
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                // Use the factory to create a builder
                DocumentBuilder builder = factory.newDocumentBuilder();
                Document doc = builder.parse(xmlFile);
                rootNode = doc.getFirstChild();
                getComDirectories(doc);
            } catch (SAXException ex) {
                print("throw SAXException ");
            } catch (IOException ex) {
                print("throw IOException");
            } catch (ParserConfigurationException ex) {
                print("throw parserconfigurationException");
            }
        }
    }

    // not in used
    private void getComDirectories(Document doc){
        rootNode = doc.getFirstChild();
        System.out.println(rootNode.getNodeName());
        //System.out.println(node.getNodeValue());
       // getFiles(rootNode);
    }


    public Node getRootNode() {
        return rootNode;
    }

    // not in used
    private void getFiles(Node node) {
        String value = null;
        Node tempNode = null;
        NodeList nodeList = node.getChildNodes();
        int nodeLength = nodeList.getLength();
        if (nodeLength == 1) {
            value = nodeList.item(0).getNodeValue();
            if (!value.equals(null)) {
                // add as an file in the directory
            }
        } else if (nodeLength > 1) {
            for (int i = 1; i < nodeList.getLength(); i+=2) {
                tempNode = nodeList.item(i);
                // add thid nodevlaue as a directory 
                System.out.println(tempNode.getNodeValue());
            }
        }
        System.out.println(nodeList.getLength());
    }

    public static void main(String[] args) {
        ReadIsdXml readXml = new ReadIsdXml("E:\\ISD_component.xml");
     }

    private void print(String message){
        System.out.println(message);
    }

}
